package org.sgx.yuigwt.yui.history;

import org.sgx.yuigwt.yui.event.EventTarget;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * Provides global state management backed by an object, but with no browser
 * history integration. For actual browser history integration and back/forward
 * support, use the history-html5 or history-hash modules.
 * 
 * @see http://yuilibrary.com/yui/docs/api/classes/HistoryBase.html
 * @author sg
 * 
 */
public class HistoryBase extends EventTarget {
	protected HistoryBase() {
	}

	/**
	 * Fired when the state changes. To be notified of all state changes regardless of the History or YUI instance that generated them, subscribe to this event on Y.Global. If you would rather be notified only about changes generated by this specific History instance, subscribe to this event on the instance.
	 */
	public static final String EVENT_CHANGE="change";
	/**
	 * 
	 */
	public static final String EVENT_HASHCHANGE="hashchange"; 
	
	
	/**
	 * Adds a state entry with new values for the specified keys. By default,
	 * the new state will be merged into the existing state, and new values will
	 * override existing values. Specifying a null or undefined value will cause
	 * that key to be removed from the new state entry.
	 * 
	 * @param state
	 *            Object hash of key/value pairs.
	 * @return self for method chaining
	 */
	public native final HistoryBase add(JavaScriptObject state) /*-{
		this.add(state);
		return this;
	}-*/;

	/**
	 * Adds a state entry with new values for the specified keys. By default,
	 * the new state will be merged into the existing state, and new values will
	 * override existing values. Specifying a null or undefined value will cause
	 * that key to be removed from the new state entry.
	 * 
	 * @param state
	 *            Object hash of key/value pairs.
	 * @param merge
	 *            If true (the default), the new state will be merged into the
	 *            existing state. New values will override existing values, and
	 *            null or undefined values will be removed from the state. If
	 *            <code>false</code>, the existing state will be discarded as a
	 *            whole and the new state will take its place.
	 * @return self for method chaining
	 */
	public native final HistoryBase add(JavaScriptObject state, boolean merge) /*-{
		this.add(state, {
			"merge" : merge
		});
		return this;
	}-*/;

	/**
	 * Adds a state entry with a new value for a single key. By default, the new
	 * value will be merged into the existing state values, and will override an
	 * existing value with the same key if there is one. Specifying a null or
	 * undefined value will cause the key to be removed from the new state
	 * entry.
	 * 
	 * @param key
	 *            State parameter key.
	 * @param value
	 *            New value.
	 * @param merge
	 *            If true (the default), the new state will be merged into the
	 *            existing state. New values will override existing values, and
	 *            null or undefined values will be removed from the state. If
	 *            <code>false</code>, the existing state will be discarded as a
	 *            whole and the new state will take its place.
	 * @return
	 */
	public native final HistoryBase addValue(String key, String value, boolean merge) /*-{
		this.addValue(key, value, {
			"merge" : merge
		});
		return this;
	}-*/;
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @param merge
//	 *            If true (the default), the new state will be merged into the
//	 *            existing state. New values will override existing values, and
//	 *            null or undefined values will be removed from the state. If
//	 *            <code>false</code>, the existing state will be discarded as a
//	 *            whole and the new state will take its place.
//	 * @return
//	 */
//	public native final History addValue(String key, boolean value, boolean merge) /*-{
//		this.addValue(key, value, {
//			"merge" : merge
//		});
//		return this;
//	}-*/;
//
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @param merge
//	 *            If true (the default), the new state will be merged into the
//	 *            existing state. New values will override existing values, and
//	 *            null or undefined values will be removed from the state. If
//	 *            <code>false</code>, the existing state will be discarded as a
//	 *            whole and the new state will take its place.
//	 * @return
//	 */
//	public native final History addValue(String key, int value, boolean merge) /*-{
//		this.addValue(key, value, {
//			"merge" : merge
//		});
//		return this;
//	}-*/;
//	
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @param merge
//	 *            If true (the default), the new state will be merged into the
//	 *            existing state. New values will override existing values, and
//	 *            null or undefined values will be removed from the state. If
//	 *            <code>false</code>, the existing state will be discarded as a
//	 *            whole and the new state will take its place.
//	 * @return
//	 */
//	public native final History addValue(String key, double value, boolean merge) /*-{
//		this.addValue(key, value, {
//			"merge" : merge
//		});
//		return this;
//	}-*/;
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @param merge
//	 *            If true (the default), the new state will be merged into the
//	 *            existing state. New values will override existing values, and
//	 *            null or undefined values will be removed from the state. If
//	 *            <code>false</code>, the existing state will be discarded as a
//	 *            whole and the new state will take its place.
//	 * @return
//	 */
//	public native final History addValue(String key, JavaScriptObject value, boolean merge) /*-{
//		this.addValue(key, value, {
//			"merge" : merge
//		});
//		return this;
//	}-*/;
	/**
	 * Adds a state entry with a new value for a single key. By default, the new
	 * value will be merged into the existing state values, and will override an
	 * existing value with the same key if there is one. Specifying a null or
	 * undefined value will cause the key to be removed from the new state
	 * entry.
	 * 
	 * @param key
	 *            State parameter key.
	 * @param value
	 *            New value.
	 * @return
	 */
	public native final HistoryBase addValue(String key, String value) /*-{
		this.addValue(key, value);
		return this;
	}-*/;
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @return
//	 */
//	public native final History addValue(String key, double value) /*-{
//		this.addValue(key, value);
//		return this;
//	}-*/;
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @return
//	 */
//	public native final History addValue(String key, boolean value) /*-{
//		this.addValue(key, value);
//		return this;
//	}-*/;
//	
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @return
//	 */
//	public native final History addValue(String key, int value) /*-{
//		this.addValue(key, value);
//		return this;
//	}-*/;
//	
//	/**
//	 * Adds a state entry with a new value for a single key. By default, the new
//	 * value will be merged into the existing state values, and will override an
//	 * existing value with the same key if there is one. Specifying a null or
//	 * undefined value will cause the key to be removed from the new state
//	 * entry.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @param value
//	 *            New value.
//	 * @return
//	 */
//	public native final History addValue(String key, JavaScriptObject value) /*-{
//		this.addValue(key, value);
//		return this;
//	}-*/;
	/**
	 * Returns the current value of the state parameter specified by key, or an
	 * object hash of key/value pairs for all current state parameters if no key
	 * is specified.
	 * 
	 * @param key
	 *            State parameter key.
	 * @return Value of the specified state parameter, or an object hash of
	 *         key/value pairs for all current state parameters.
	 */
	public native final String get(String key) /*-{
		return this.get(key)+"";
	}-*/;
//	/**
//	 * Returns the current value of the state parameter specified by key, or an
//	 * object hash of key/value pairs for all current state parameters if no key
//	 * is specified.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @return Value of the specified state parameter, or an object hash of
//	 *         key/value pairs for all current state parameters.
//	 */
//	public native final String getString(String key) /*-{
//		return this.get(key)+"";
//	}-*/; 
//	/**
//	 * Returns the current value of the state parameter specified by key, or an
//	 * object hash of key/value pairs for all current state parameters if no key
//	 * is specified.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @return Value of the specified state parameter, or an object hash of
//	 *         key/value pairs for all current state parameters.
//	 */
//	public native final int getInt(String key) /*-{
//		return this.get(key) || 0;
//	}-*/;
//	/**
//	 * Returns the current value of the state parameter specified by key, or an
//	 * object hash of key/value pairs for all current state parameters if no key
//	 * is specified.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @return Value of the specified state parameter, or an object hash of
//	 *         key/value pairs for all current state parameters.
//	 */
//	public native final double getDouble(String key) /*-{
//		return this.get(key) || 0;
//	}-*/;
//	/**
//	 * Returns the current value of the state parameter specified by key, or an
//	 * object hash of key/value pairs for all current state parameters if no key
//	 * is specified.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @return Value of the specified state parameter, or an object hash of
//	 *         key/value pairs for all current state parameters.
//	 */
//	public native final boolean getBoolean(String key) /*-{
//		return this.get(key) || false;
//	}-*/;
//	/**
//	 * Returns the current value of the state parameter specified by key, or an
//	 * object hash of key/value pairs for all current state parameters if no key
//	 * is specified.
//	 * 
//	 * @param key
//	 *            State parameter key.
//	 * @return Value of the specified state parameter, or an object hash of
//	 *         key/value pairs for all current state parameters.
//	 */
//	public native final JavaScriptObject getObj(String key) /*-{
//		return this.get(key) || null;
//	}-*/;
	/**
	 * Returns the current value of the state parameter specified by key, or an
	 * object hash of key/value pairs for all current state parameters if no key
	 * is specified.
	 * 
	 * @param key
	 *            State parameter key.
	 * @return Value of the specified state parameter, or an object hash of
	 *         key/value pairs for all current state parameters.
	 */
	public native final JavaScriptObject get() /*-{
		return this.get();
	}-*/;

	/**
	 * Same as add() except that a new browser history entry will not be
	 * created. Instead, the current history entry will be replaced with the new
	 * state.
	 * 
	 * @param state
	 *            Object hash of key/value pairs.
	 * @return self for method chaining
	 */
	public native final HistoryBase replace(JavaScriptObject state) /*-{
		this.replace(state);
		return this;
	}-*/;

	/**
	 * Same as add() except that a new browser history entry will not be
	 * created. Instead, the current history entry will be replaced with the new
	 * state.
	 * 
	 * @param state
	 *            Object hash of key/value pairs.
	 * @param merge
	 *            If true (the default), the new state will be merged into the
	 *            existing state. New values will override existing values, and
	 *            null or undefined values will be removed from the state. If
	 *            <code>false</code>, the existing state will be discarded as a
	 *            whole and the new state will take its place.
	 * @return self for method chaining
	 */
	public native final HistoryBase replace(JavaScriptObject state, boolean merge) /*-{
		this.replace(state, {
			"merge" : merge
		});
		return this;
	}-*/;

	/**
	 * Same as addValue() except that a new browser history entry will not be
	 * created. Instead, the current history entry will be replaced with the new
	 * state.
	 * 
	 * @param key
	 *            State parameter key.
	 * @param value
	 *            New value.
	 * @param merge
	 *            If true (the default), the new state will be merged into the
	 *            existing state. New values will override existing values, and
	 *            null or undefined values will be removed from the state. If
	 *            <code>false</code>, the existing state will be discarded as a
	 *            whole and the new state will take its place.
	 * @return
	 */
	public native final HistoryBase replaceValue(String key, String value,
			boolean merge) /*-{
		this.replaceValue(key, value, {
			"merge" : merge
		});
		return this;
	}-*/;

	/**
	 * Same as addValue() except that a new browser history entry will not be
	 * created. Instead, the current history entry will be replaced with the new
	 * state.
	 * 
	 * @param key
	 *            State parameter key.
	 * @param value
	 *            New value.
	 * @return
	 */
	public native final HistoryBase replaceValue(String key, String value) /*-{
		this.replaceValue(key, value);
		return this;
	}-*/;

}
